<?php
function echoIllnesses() {
	$health = simplexml_load_file("db/illness.xml");

	foreach($health->illness as $illness){
		foreach($illness->symptom as $symptom){
		
			if ( $symptom == $_REQUEST['abi']) {
				echo "Name: ", htmlentities((string) $illness->name);
				echo "Symptoms: ", htmlentities((string) $illness->symptoms);
				echo "Treatment:  ", htmlentities((string) $illness->treatment);
				echo "Precaution: ", htmlentities((string) $illness->precaution), "\n";
			}
		}
	}
}

function getIllness() {
	$health = simplexml_load_file("db/illness.xml");
	$map = array();

	foreach($health->illness as $illness){
		foreach($illness->symptom as $symptom){
			$symptom = (string)$symptom;
			if (in_array($symptom, $map)) {
				$map[$symptom]++;
			} else {
				$map[$symptom] = 1;
			}
		}
	}

	$maximum = 0;
	$sympt;
	foreach($map as $symptom => $count){
		if ($count > $maximum) {
			$maximum = $count;
			$sympt = $symptom;
		}
	}

	$_SESSION['fevermode'] = TRUE;
	$_SESSION['symptoms'] = array();
	$_SESSION['symptom_current'] = $sympt;

	return 'Do you have: '.$sympt.'?';
}

function hasSymptom() {
	$_SESSION['symptoms'][] = $_SESSION['symptom_current'];

	$health = simplexml_load_file("db/illness.xml");
	$map = array();

	foreach($health->illness as $illness){
		$symptoms_match = $_SESSION['symptoms'];
		$symptoms_count = count($symptoms_match);

		foreach($illness->symptom as $symptom){
			$symptom = (string)$symptom;
			if (in_array($symptom, $symptoms_match)) {
				unset($symptoms_match[$symptom]);
				$symptoms_count--;
			}
		}

		if ($symptoms_count == 0) {
			foreach($illness->symptom as $symptom){
				$symptom = (string)$symptom;
				if (!in_array($symptom, $_SESSION['symptoms'])) {
					if (array_key_exists($symptom, $map)) {
						$map[$symptom]++;
					} else {
						$map[$symptom] = 1;
					}
				}
			}
		}
	}

	$maximum = 0;
	$sympt;
	foreach($map as $symptom => $count){
		if ($count > $maximum) {
			$maximum = $count;
			$sympt = $symptom;
		}
	}

	$_SESSION['symptom_current'] = $sympt;

	return 'Do you have '.$sympt.'?'.$maximum;
}

?>
